#include "mainscene.h"
#include "fuseitem.h"
#include "chip.h"
#include "resistor.h"
#include "sensor.h"
#include "ControlCAN.h"
#include "maindeviceform.h"
#include <QGlobalStatic>

Q_GLOBAL_STATIC(MainScene,m_instance)

MainScene::MainScene(QObject *parent)
    : QGraphicsScene(parent)
{
    setupCircuit();
    m_canData = QByteArray(8,0x00);
}

MainScene *MainScene::Instance()
{
    return m_instance;
}

void MainScene::SetVolterB(double value)
{
    m_vmb->setValue(value);
}

void MainScene::SetVolterP(double value)
{
    m_vmp->setValue(value);
}

void MainScene::SetEnableRelay(bool relayEnable)
{
    for(auto relay : m_relays) {
        relay->setEnable(relayEnable);
    }
}

void MainScene::SetFSState(bool state)
{
    relay_14->setClosed(state);
}

QByteArray MainScene::GetCurrentByte()
{
    return m_canData;
}

void MainScene::SetByte(const QByteArray &canData)
{
    m_canData = canData;
}

void MainScene::setupCircuit()
{    
    // 创建元件
    m_vmb = new VoltageMeter(0.0);
    m_vmb->setPos(-600, -450);
    m_vmb->setText("B+电压采集点");
    addItem(m_vmb);

    Fuse *fuse = new Fuse(false);
    fuse->setPos(-500, -450);
    fuse->setText("主熔断器");
    addItem(fuse);

    relay = new Relay(false);
    relay->setPos(-350, -450);
    relay->setText("主正继电器");
    connect(relay,&Relay::changeStateMainZ,this,&MainScene::on_changeState_MainZ);
    addItem(relay);
    m_relays.append(relay);

    m_vmp = new VoltageMeter(0.0);
    m_vmp->setPos(-200, -450);
    m_vmp->setText("P+电压采集点");
    addItem(m_vmp);
    // 创建连线
    createWire(m_vmb,CircuitComponent::OutputPort, fuse,CircuitComponent::InputPort);
    createWire(fuse,CircuitComponent::OutputPort, relay,CircuitComponent::InputPort);
    createWire(relay,CircuitComponent::OutputPort, m_vmp,CircuitComponent::InputPort);

    relay_1 = new Relay(false);   //预充继电器
    relay_1->setPos(-390, -370);
    relay_1->setText("预充继电器");
    connect(relay_1,&Relay::changeStatePrevC,this,&MainScene::on_changeState_PrevC);
    addItem(relay_1);
    m_relays.append(relay_1);

    Resistor *resistor = new Resistor();
    resistor->setPos(-210, -370);
    resistor->setText("预充电阻");
    addItem(resistor);

    createWire(fuse,CircuitComponent::OutputPort, relay_1,CircuitComponent::InputPort);
    createWire(relay_1,CircuitComponent::OutputPort, resistor,CircuitComponent::InputPort);

//    Fuse *fuse_2 = new Fuse(false);   //充电保险丝
//    fuse_2->setPos(-450, -320);
//    fuse_2->setText("充电保险丝");
//    addItem(fuse_2);

    relay_2 = new Relay(false);   //快充继电器
    relay_2->setPos(-340, -320);
    relay_2->setText("充电继电器");
    connect(relay_2,&Relay::changeStateFastC,this,&MainScene::on_changeState_FastC);
    addItem(relay_2);
    m_relays.append(relay_2);

    VoltageMeter *vm_CH = new VoltageMeter(0.0);
    vm_CH->setPos(-200, -320);
    vm_CH->setText("充电电压采集点");
    addItem(vm_CH);

    createWire(m_vmb,CircuitComponent::OutputPort, relay_2,CircuitComponent::InputPort);
    //createWire(fuse_2,CircuitComponent::OutputPort, relay_2,CircuitComponent::InputPort);
    createWire(relay_2,CircuitComponent::OutputPort, vm_CH,CircuitComponent::InputPort);

    VoltageMeter *vm_B_ = new VoltageMeter(0.0);
    vm_B_->setPos(-460, -240);
    vm_B_->setText("B-电压采集点");
    addItem(vm_B_);

//    Sensor *sensor = new Sensor();
//    sensor->setPos(-430, -240);
//    sensor->setText("霍尔电流传感器");
//    addItem(sensor);

    relay_main_ = new Relay(false);
    relay_main_->setPos(-300, -240);
    relay_main_->setText("主负继电器");
    connect(relay_main_,&Relay::changeStateMainF,this,&MainScene::on_changeState_MainF);
    addItem(relay_main_);
    m_relays.append(relay_main_);

    VoltageMeter *vm_P_ = new VoltageMeter(0.0);
    vm_P_->setPos(-200, -240);
    vm_P_->setText("P-电压采集点");
    addItem(vm_P_);

    createWire(vm_B_,CircuitComponent::OutputPort,relay_main_,CircuitComponent::InputPort);
    //createWire(sensor,CircuitComponent::OutputPort, relay_main_,CircuitComponent::InputPort);
    createWire(relay_main_,CircuitComponent::OutputPort, vm_P_,CircuitComponent::InputPort);

    relay_14 = new Relay(false);
    relay_14->setPos(-400, -150);
    relay_14->setText("风扇继电器");
    //connect(MainDeviceFormIns,&MainDeviceForm::changeFSState,this,&MainScene::on_changeState_FengS);
    addItem(relay_14);    
    m_relays.append(relay_14);

//    Fuse *fuse_u25 = new Fuse(false);
//    fuse_u25->setPos(-200, -150);
//    fuse_u25->setText("自恢复保险丝");
//    addItem(fuse_u25);

//    createWire(relay_14,CircuitComponent::OutputPort,fuse_u25,CircuitComponent::InputPort);
    //createWire(fuse_u25,CircuitComponent::OutputPort,m_vmp,CircuitComponent::OutputPort);

    VoltageMeter *vmb_b_ = new VoltageMeter(0.0);
    vmb_b_->setPos(-550, -345);
    vmb_b_->setText("绝缘检测");
    addItem(vmb_b_);
    createWire(m_vmb,CircuitComponent::InputPort,vmb_b_,CircuitComponent::InputPort);
    createWire(vmb_b_,CircuitComponent::OutputPort,vm_B_,CircuitComponent::InputPort);
    //createWire(m_vmp,CircuitComponent::OutputPort,vm_CH,CircuitComponent::OutputPort);
    //createWire(m_vmp,CircuitComponent::OutputPort,vm_P_,CircuitComponent::OutputPort);
    //createWire(m_vmp,CircuitComponent::OutputPort,resistor,CircuitComponent::OutputPort);
}

void MainScene::createWire(QGraphicsItem *startItem, CircuitComponent::PortType startPortType, QGraphicsItem *endItem, CircuitComponent::PortType endPortType)
{
    CircuitComponent *startComp = dynamic_cast<CircuitComponent*>(startItem);
    CircuitComponent *endComp = dynamic_cast<CircuitComponent*>(endItem);

    if(startComp && endComp) {
        QPointF startPos = startComp->mapToScene(
            startComp->portPosition(startPortType));
        QPointF endPos = endComp->mapToScene(
            endComp->portPosition(endPortType));

        Wire* wire = new Wire(startPos, endPos);
        wire->setColor(validateConnection(startPortType, endPortType));

        addItem(wire);
    }
}

// 新增连线验证方法
QColor MainScene::validateConnection(CircuitComponent::PortType start, CircuitComponent::PortType end) {
    /* 返回颜色表示连接类型：
     * 蓝：正常输出->输入
     * 绿：输入->输入
     * 红：输出->输出
     * 黄：输入->输出（非常规）*/
    if(start == CircuitComponent::OutputPort && end == CircuitComponent::InputPort) return QColor("#bdc3c7");
    if(start == CircuitComponent::InputPort && end == CircuitComponent::InputPort) return QColor("#bdc3c7");
    if(start == CircuitComponent::OutputPort && end == CircuitComponent::OutputPort) return QColor("#bdc3c7");
    return Qt::yellow;
}

void MainScene::on_changeState_MainZ(bool state)
{
    //初始化8字节全是0
    if(state) {
        m_canData[0] = 0x01;
    }
    else {
        m_canData[0] = 0x00;
    }
    emit sendData(0x37B,m_canData);
}

void MainScene::on_changeState_MainF(bool state)
{
    if(state) {
        m_canData[3] = 0x01;
    }
    else {
        m_canData[3] = 0x00;
    }
    emit sendData(0x37B,m_canData);
}

void MainScene::on_changeState_FastC(bool state)
{
    if(state) {
        m_canData[4] = 0x01;
    }
    else {
        m_canData[4] = 0x00;
    }
    emit sendData(0x37B,m_canData);
}

void MainScene::on_changeState_PrevC(bool state)
{
    if(state) {
        m_canData[6] = 0x01;
    }
    else {
        m_canData[6] = 0x00;
    }
    emit sendData(0x37B,m_canData);
}
